from flask import render_template, request

from blueprints import block_blueprint
from models import QueryMultipleInstances, Blog, PackInstance, Comment, User, QueryOneInstance, Personal, Role
from tools.decorate import pack_session


@block_blueprint.route('/public/page/blocks', methods=['GET'])
@pack_session
def get_public_page_of_blocks(data):
    category_map = {
        'friend': '交友',
        'help': '求助',
        'consultation': '咨询',
        'transaction': '交易',
        'service': '服务'
    }

    data.update({key: [] for key in category_map})

    for key, zh_category in category_map.items():
        blogs = QueryMultipleInstances(Blog, {'sort': zh_category, 'checked': True, 'passed': True,'role':False,'part':False})

        category_data = []
        for blog in blogs:
            comments = QueryMultipleInstances(Comment, {'pid': blog.id})  # 获取评论
            author = QueryOneInstance(User, {'id': blog.uid})  # 获取作者
            author_personal = QueryOneInstance(Personal, {'uid': author.id})  # 获取作者的个人信息

            blog_data={}
            blog_data['Comment']=[]
            if blog:
                blog_data.update(PackInstance(blog))

            if author:
                blog_data.update(PackInstance(author))

            if author_personal:
                blog_data.update(PackInstance(author_personal))

            if comments:
                blog_data.update(PackInstance(comments))

            blog_data['writeable'] = True

            for i in blog_data['Comment']:
                personal = QueryOneInstance(Personal, {'uid': i['uid']})
                user = QueryOneInstance(User, {'id': i['uid']})
                if personal:
                    i.update(PackInstance(personal))

                if user:
                    i.update(PackInstance(user))

            category_data.append(blog_data)
        data[key] = category_data  # 按英文分类存储数据，但查询时用中文

    return render_template('pages/blocks/blog.html', data=data)

@block_blueprint.route('/public/page/blocks/role', methods=['GET'])
@pack_session
def get_public_page_of_blocks_role(data):
    category_map = {
        'friend': '交友',
        'help': '求助',
        'consultation': '咨询',
        'transaction': '交易',
        'service': '服务'
    }

    data.update({key: [] for key in category_map})

    for key, zh_category in category_map.items():
        blogs = QueryMultipleInstances(Blog, {'sort': zh_category, 'checked': True, 'passed': True,'role':True})

        category_data = []
        for blog in blogs:
            role1 = QueryOneInstance(Role, {'uid': blog.uid})
            role2 = QueryOneInstance(Role, {'uid': data['session']['uid']})
            if role1.role != role2.role:
                continue

            comments = QueryMultipleInstances(Comment, {'pid': blog.id})  # 获取评论
            author = QueryOneInstance(User, {'id': blog.uid})  # 获取作者
            author_personal = QueryOneInstance(Personal, {'uid': author.id})  # 获取作者的个人信息

            blog_data={}
            blog_data['Comment']=[]
            if blog:
                blog_data.update(PackInstance(blog))

            if author:
                blog_data.update(PackInstance(author))

            if author_personal:
                blog_data.update(PackInstance(author_personal))

            if comments:
                blog_data.update(PackInstance(comments))

            blog_data['writeable'] = True

            for i in blog_data['Comment']:
                personal = QueryOneInstance(Personal, {'uid': i['uid']})
                user = QueryOneInstance(User, {'id': i['uid']})
                if personal:
                    i.update(PackInstance(personal))

                if user:
                    i.update(PackInstance(user))

            category_data.append(blog_data)
        data[key] = category_data  # 按英文分类存储数据，但查询时用中文

    return render_template('pages/blocks/blog.html', data=data)


@block_blueprint.route('/public/page/blocks/part', methods=['GET'])
@pack_session
def get_public_page_of_blocks_part(data):
    category_map = {
        'friend': '交友',
        'help': '求助',
        'consultation': '咨询',
        'transaction': '交易',
        'service': '服务'
    }

    data.update({key: [] for key in category_map})

    for key, zh_category in category_map.items():
        blogs = QueryMultipleInstances(Blog, {'sort': zh_category, 'checked': True, 'passed': True, 'part': True})

        category_data = []
        for blog in blogs:
            part1 = QueryOneInstance(Role, {'uid': blog.uid})
            part2 = QueryOneInstance(Role, {'uid': data['session']['uid']})
            if part1.part != part2.part:
                continue

            comments = QueryMultipleInstances(Comment, {'pid': blog.id})  # 获取评论
            author = QueryOneInstance(User, {'id': blog.uid})  # 获取作者
            author_personal = QueryOneInstance(Personal, {'uid': author.id})  # 获取作者的个人信息

            blog_data = {}
            blog_data['Comment'] = []
            if blog:
                blog_data.update(PackInstance(blog))

            if author:
                blog_data.update(PackInstance(author))

            if author_personal:
                blog_data.update(PackInstance(author_personal))

            if comments:
                blog_data.update(PackInstance(comments))

            blog_data['writeable'] = True

            for i in blog_data['Comment']:
                personal = QueryOneInstance(Personal, {'uid': i['uid']})
                user = QueryOneInstance(User, {'id': i['uid']})
                if personal:
                    i.update(PackInstance(personal))

                if user:
                    i.update(PackInstance(user))

            category_data.append(blog_data)
        data[key] = category_data  # 按英文分类存储数据，但查询时用中文

    return render_template('pages/blocks/blog.html', data=data)

